#pragma once
#include <iostream>
#include <cerrno>
#include <cstring>
#include "tcpserver.hpp"
#include "threadpool.hpp"
#include "Task.hpp"

#define defaultPort 8080

class tcpserver;

class httpserver
{
private:
    int _port;

public:
    httpserver(int port = defaultPort) : _port(port)
    {
    }
    void Loop()
    {
        tcpserver *svr = tcpserver::Getinstance(_port);
        threadpool *tp = threadpool::GetInstance(5);
        while (true)
        {
            struct sockaddr_in peer;
            socklen_t len = sizeof(peer);
            int fd = accept(svr->Fd(), (struct sockaddr *)&peer, &len);
            if (fd < 0)
            {
                Log(WARNING, "accpet error, errno: %d, errstr: %s", errno, strerror(errno));
                continue;
            }

            // pthread_t thread;
            // int* newfd = new int(fd);
            // pthread_create(&thread, nullptr, Entrance::HandlerRequest, newfd);
            // pthread_detach(thread);
            Task task(fd);
            tp->Push(task);
        }
    }
    ~httpserver()
    {
    }
};
